home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: howland.reston.ans.net!torn!nott!emr1!jagrant
- From: jagrant@emr1.emr.ca (John Grant)
- Subject: looping & sscanf
- Message-ID: <DLtqMw.8Ao@emr1.emr.ca>
- Organization: Energy, Mines, and Resources, Ottawa
- Date: Sat, 27 Jan 1996 05:16:08 GMT
-
- In FORTRAN, I can read a set of numbers from a character string with an
- 'internal read':
- character*100 buffer
- real x(10)
- read(buffer,*) (x(i),i=1,n)
-
- How can I do this easily in C? If I was reading directly from a file, I
- would use:
- for(i=0;i<n;i++){
- fscanf(f,"%g",&x[i]);
- }
-
- However, I want to read the values from a string, i.e. using atof() or
- sscanf() or ???.
-
- I was hoping to do something like:
- char buffer[100];
- char *b=buffer;
- for(i=0;i<n;i++){
- sscanf(b,"%g",&x[i]);
- b+=???;
- }
- but I can't figure out how much to advance the pointer.
-
- Perhaps I should loop through the string using strtok(), converting each
- substring using atof()? It seems a bit messy, well at least not as
- straightforward and simple as FORTRAN.
-
- I'm sure it's straightforward, but I just can't see it (or find it in the FAQ).
- --
- John A. Grant jagrant@emr1.emr.ca
- Airborne Geophysics
- Geological Survey of Canada, Ottawa
-